package com.itheima.service.sms.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.core.model.PageResult;
import com.itheima.common.exception.CustomException;
import com.itheima.mapper.sms.CouponMapper;
import com.itheima.pojo.sms.Coupon;
import com.itheima.pojo.sms.CouponProductCategoryRelation;
import com.itheima.pojo.sms.CouponProductRelation;
import com.itheima.service.sms.CouponProductCategoryRelationService;
import com.itheima.service.sms.CouponProductRelationService;
import com.itheima.service.sms.CouponService;

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;

@DubboService
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements CouponService {


    @Autowired
    private CouponProductCategoryRelationService couponProductCategoryRelationService;

    @Autowired
    private CouponProductRelationService couponProductRelationService;

    /**
     * 添加优惠券
     *
     * @param couponParam
     * @return
     */
    @Override
    public int create(Coupon couponParam) {
        //非空判断
        if (StringUtils.isEmpty(couponParam)) {
            throw new CustomException("请正确输入！");
        }
        boolean result = this.save(couponParam);
        //拿到优惠券绑定的商品分类
        List<CouponProductCategoryRelation> productCategoryRelationList = couponParam.getProductCategoryRelationList();
        //遍历集合
        for (CouponProductCategoryRelation couponProductCategoryRelation : productCategoryRelationList) {
            //添加到优惠卷绑定的商品分类表
            if (!StringUtils.isEmpty(couponProductCategoryRelation)) {
                couponProductCategoryRelation.setCouponId(couponParam.getId());
                couponProductCategoryRelationService.save(couponProductCategoryRelation);
            }
        }
        //拿到优惠券绑定的商品
        List<CouponProductRelation> productRelationList = couponParam.getProductRelationList();
        //遍历集合
        for (CouponProductRelation couponProductRelation : productRelationList) {
            if (!StringUtils.isEmpty(couponProductRelation)) {
                couponProductRelation.setCouponId(couponParam.getId());
                couponProductRelationService.save(couponProductRelation);
            }
        }
        return result ? 1 : 0;
    }

    /**
     * 删除优惠券
     *
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) {
        //删除优惠卷关联的商品分类信息
        LambdaQueryWrapper<CouponProductCategoryRelation> lqw=new LambdaQueryWrapper<>();
        lqw.eq(CouponProductCategoryRelation::getCouponId,id);
        couponProductCategoryRelationService.remove(lqw);
        //删除优惠卷关联的商品信息
        LambdaQueryWrapper<CouponProductRelation> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CouponProductRelation::getCouponId,id);
        couponProductRelationService.remove(lambdaQueryWrapper);
        //删除优惠卷
        boolean result = this.removeById(id);
        return result ? 1 : 0;
    }
    /**
     * 根据优惠券名称和类型分页获取优惠券列表
     * @return
     */
    @Override
    public PageResult select(String name, Integer pageNum, Integer pageSize, Integer type) {
        //构造分页构造器
        Page<Coupon> pageInfo=new Page<>(pageNum,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Coupon> lqw =new LambdaQueryWrapper<>();
        //模糊查询
        lqw.like(!StringUtils.isEmpty(name),Coupon::getName,name);
        lqw.eq(Coupon::getType,type);
        this.page(pageInfo,lqw);
        return PageResult.init((int) pageInfo.getTotal(), pageSize, pageNum, pageInfo.getRecords());
    }
    /**
     * 修改优惠券
     * @param id
     * @param couponParam
     * @return
     */
    @Override
    public int updateCoupon(Long id, Coupon couponParam) {
        if (couponParam==null){
            throw  new CustomException("暂无数据");
        }
        //修改优惠卷关联的商品分类
        List<CouponProductCategoryRelation> productCategoryRelationList = couponParam.getProductCategoryRelationList();
        for (CouponProductCategoryRelation couponProductCategoryRelation : productCategoryRelationList) {
            if (couponProductCategoryRelation!=null){
                couponProductCategoryRelationService.updateById(couponProductCategoryRelation);
            }
        }
        //修改优惠卷关联的商品
        List<CouponProductRelation> productRelationList = couponParam.getProductRelationList();
        for (CouponProductRelation couponProductRelation : productRelationList) {
            if (couponProductRelation!=null){
                couponProductRelationService.updateById(couponProductRelation);
            }
        }
        //修改优惠卷
        LambdaQueryWrapper<Coupon> lqw =new LambdaQueryWrapper<>();
        lqw.eq(id!=null,Coupon::getId,id);
        boolean result = this.update(couponParam, lqw);

        return result?1:0;
    }
    /**
     * 获取单个优惠券的详细信息
     * @param id
     * @return
     */
    @Override
    public Coupon selectById(Long id) {
        //查询优惠卷详情
        Coupon coupon = this.getById(id);
        //查询优惠卷关联的商品分类信息
        LambdaQueryWrapper<CouponProductCategoryRelation> lqw=new LambdaQueryWrapper<>();
        lqw.eq(CouponProductCategoryRelation::getCouponId,id);
        List<CouponProductCategoryRelation> couponProductCategoryRelationList = couponProductCategoryRelationService.list(lqw);
        //结果赋值给coupon
        coupon.setProductCategoryRelationList(couponProductCategoryRelationList);
        //查询优惠卷关联的商品信息
        LambdaQueryWrapper<CouponProductRelation> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CouponProductRelation::getCouponId,id);
        List<CouponProductRelation> couponProductRelations = couponProductRelationService.list(lambdaQueryWrapper);
        //结果赋值给coupon
        coupon.setProductRelationList(couponProductRelations);
        return coupon;
    }
}
